#include "minitranslator.h"
#include "ui_minitranslator.h"
#include "transliterator.h"

MiniTranslator::MiniTranslator(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MiniTranslator)
{
    ui->setupUi(this);
    connect(ui->fromCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotFromChanged(int)));
    connect(ui->toCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotToChanged(int)));
    ui->resultText->setFont(QFont("Sanskrit 2003", 12));
}

MiniTranslator::~MiniTranslator()
{
    delete ui;
}

void MiniTranslator::slotFromChanged(int i)
{
    if(i == 5)
        ui->sourceText->setFont(QFont("Sanskrit 2003", 12));
    else
        ui->sourceText->setFont(QFont("DejaVu Sans", 10));
    on_transBtn_clicked();
}

void MiniTranslator::slotToChanged(int i)
{
    if(i == 0)
        ui->resultText->setFont(QFont("Sanskrit 2003", 12));
    else
        ui->resultText->setFont(QFont("DejaVu Sans", 10));
    on_transBtn_clicked();
}

void MiniTranslator::on_transBtn_clicked()
{
    Transliterator *transor = new Transliterator();

    QString fromText = ui->sourceText->toPlainText();
    int from = ui->fromCombo->currentIndex();
    int to = ui->toCombo->currentIndex();
    QString itrans;
    switch(from) {
    case 0:
        itrans = transor->slp1_to_itrans(fromText);
        break;
    case 1:
        itrans = fromText;
        break;
    case 2:
        itrans = transor->hk_to_itrans(fromText);
        break;
    case 3:
        itrans = transor->velthuis_to_itrans(fromText);
        break;
    case 4:
        itrans = transor->iast_to_itrans(fromText);
        break;
    case 5:
        itrans = transor->deva_to_itrans(fromText);
        break;
    }

    QString result;
    switch (to) {
    case 0:
        result = transor->itrans_to_deva(itrans);
        break;
    case 1:
        result = transor->itrans_to_iast(itrans);
        break;
    case 2:
        result = transor->itrans_to_iso15919(itrans);
        break;
    case 3:
        result = transor->itrans_to_slp1(itrans);
        break;
    case 4:
        result = itrans;
        break;
    case 5:
        result = transor->itrans_to_hk(itrans);
        break;
    case 6:
        result = transor->itrans_to_velthuis(itrans);
        break;
    }

    ui->resultText->setText(result);
}
